package cn.com.dhc.zzywb.startspring.demo.controller;

import cn.com.dhc.zzywb.startspring.demo.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class RoleController {

    private static List<User> roles = new ArrayList(){
        {
            User u1=new User();
            u1.setId(1);
            u1.setName("百度运营主管");
            u1.setText("");
            add(u1);

            User u2=new User();
            u2.setId(2);
            u2.setName("OEM运营主管");
            u2.setText("");
            add(u2);

            User u3=new User();
            u3.setId(3);
            u3.setName("vczh");
            u3.setText("知乎大V");
            add(u3);
        }

    };
    //http://localhost:8888/roles?name=百度运营主管
    @RequestMapping("/roles")
    public List<User> index(@RequestParam(required  = true) String name){
        System.out.println(roles.stream().filter(r->name.equals(r.getName())).collect(Collectors.toList()).toString() );
        return roles.stream().filter(r->name.equals(r.getName())).collect(Collectors.toList());
    }
    //http://localhost:8888/roles/show/id/2
    @GetMapping("/roles/show/id/{id}")
    public User show(@PathVariable Integer id){
        System.out.println(roles.stream().filter(r->id.equals(r.getId())).findFirst().get().toString() );
        return roles.stream().filter(r->id.equals(r.getId())).findFirst().get();
    }
}
